#include "Scene/Transformable.h"

namespace vital {

	//--------------------------------------------------------------------------
	TTransformable::TTransformable() {

		resetXForm();
	}

	//--------------------------------------------------------------------------
	TTransformable::~TTransformable() {
	}

	//--------------------------------------------------------------------------
	TMtx4 TTransformable::getWorldTransform() {

		if(mXFormDirty) {

			mXForm = 
				translate(mTrans) * 
				rotateYX(mRot[0], mRot[1]) * 
				scale(mScale, mScale, mScale);;

			// todo update the matrix
			mXFormDirty = false;
		}

		return mXForm;

	}

	//--------------------------------------------------------------------------
	void TTransformable::moveAbs(const TVec3 &val) {

		mTrans += val;
		mXFormDirty = true;
	}

	//--------------------------------------------------------------------------
	void TTransformable::moveRel(const TVec3 &val) {

		mTrans += get3x3( getWorldTransform() ) * val;
		mXFormDirty = true;
	}

	//--------------------------------------------------------------------------
	void TTransformable::yaw(float val) {

		mRot[1] += val;
		mXFormDirty = true;
	}

	//--------------------------------------------------------------------------
	void TTransformable::pitch(float val) {

		mRot[0] += val;
		mXFormDirty = true;
	}

	//--------------------------------------------------------------------------
	void TTransformable::roll(float val) {

		mRot[2] += val;
		mXFormDirty = true;
	}

	//--------------------------------------------------------------------------
	void TTransformable::magnify(float val) {

		mScale += val;
		mScale = Math::Max(mScale, 0.0f);
		mXFormDirty = true;
	}

	//--------------------------------------------------------------------------
	void TTransformable::resetXForm() {

		mTrans = TVec3(0.0f, 0.0f, 0.0f);
		mRot = TVec3(0.0f, 0.0f, 0.0f);
		mScale = 1.0f;

		mXForm = identity4();
		mXFormDirty = false;
	}

	//--------------------------------------------------------------------------

}